﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Xsl;
using Microsoft.Office.Interop.Word;
using UseCaseManager.BO;

namespace UseCaseManager.Logic
{
    // The process outlined in (Jon's blog post) using xml, xslt and word
    // is used. This option works successfully(see accompanying tests)
    // Project can be exported to word doc. Project Table can be nserted into word doc
    // Created a macro to perform the copy=paste to bookmark- translated to C#
    public class WriteDocuments
    {
        public static void WriteToXmlFile(Project project, string xmlFileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Project));
            StreamWriter sw = new StreamWriter(xmlFileName);
            try
            {
                xs.Serialize(sw, project);
            }
            finally
            {
                sw.Close();
            }
        }

        public static void WriteProjectToWordDocument(string xmlFileName, string xslFileName, string docFileName)
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(xslFileName);
            xslt.Transform(xmlFileName, docFileName);
        }

        public static void WriteRequirementsToWordDocument(string projectDocName, string requirementsDocName, string generatedDocName, string insertionPointBookmark)
        {
            
            object missing = Type.Missing;
            _Application oWord = new Application { Visible = false };
            _Document oDoc;
            _Document projectDoc;

            try
            {
                object fileName = Environment.CurrentDirectory + Path.DirectorySeparatorChar + requirementsDocName;
                oDoc = oWord.Documents.Open(ref fileName, ref missing, ref missing, ref missing,
                                             ref missing, ref missing, ref missing, ref missing,
                                             ref missing, ref missing, ref missing, ref missing,
                                             ref missing, ref missing, ref missing, ref missing);

                object projectFileName = Path.Combine(Environment.CurrentDirectory, projectDocName);
                projectDoc = oWord.Documents.Open(ref projectFileName,
                                                  ref missing, ref missing, ref missing, ref missing,
                                                  ref missing, ref missing, ref missing, ref missing,
                                                  ref missing, ref missing, ref missing, ref missing,
                                                  ref missing, ref missing, ref missing);
                projectDoc.Activate();

                oWord.Selection.WholeStory();
                oWord.Selection.Copy();

                oDoc.Activate();

                object insertAtBookmark = insertionPointBookmark;

                oWord.Selection.GoTo(ref missing, ref missing, ref missing, ref insertAtBookmark);
                oWord.Selection.Paste();

                object requirementsFileName = Environment.CurrentDirectory + Path.DirectorySeparatorChar +
                                              generatedDocName;
                oDoc.SaveAs(ref requirementsFileName, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing);
            }
            finally
            {
                oWord.Quit(ref missing, ref missing, ref missing);
            }
        }
    }
}
